//=============================================================================
// System  : MatchKit
// File    : CharPattern.cs
// Author  : Fabio Bussu  (matchkit@fabiobussu.it)
// Updated : 04/16/2013
// Note    : Copyright 2013, Fabio Bussu, All rights reserved
//
// This code is published under the Microsoft Public License (Ms-PL). A copy
// of the license should be distributed with the code.  It can also be found
// at the project website: http://matchkit.codeplex.com. This notice, the
// author's name, and all copyright notices must remain intact in all
// applications, documentation, and source files.
//
// Version     Date     Who  Comments
// ============================================================================
// 1.0.0.0  04/16/2013   FB  First draft
//=============================================================================
using System;
using System.Runtime.Serialization;

namespace MatchKit.Patterns
{
	/// <summary>
	/// Provides the logic to match a single character or a specific character.
	/// </summary>
	/// <threadsafety static="true" instance="false" />
	/// <serialization binary="true" contract="true" />
	/// <conceptualLink target="cb785773-b793-4cc4-acc4-6688cdb026b6" />
	/// <conceptualLink target="0a06b2a9-5f58-409a-ba62-639cfd77bca7" />
	#if DC
	[DataContract]
	#endif
	#if SZ
	[Serializable]
	#endif
	public sealed class CharPattern : BasePattern
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="T:MatchKit.Patterns.CharPattern"/> class. 
		/// </summary>
		public CharPattern()
		{
			Character = '\0';
			IgnoreCase = true;
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="T:MatchKit.Patterns.CharPattern"/> class. 
		/// </summary>
		/// <param name="character">The character to match. See <see cref="P:CharPattern.Character"/>.</param>
		public CharPattern(char character)
			: this(character, true)
		{
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="T:MatchKit.Patterns.CharPattern"/> class. 
		/// </summary>
		/// <param name="character">The character to match. See <see cref="P:CharPattern.Character"/>.</param>
		/// <param name="ignoreCase">Specifies whether to perform a case insensitive comparison. See <see cref="P:CharPattern.IgnoreCase"/>.</param>
		public CharPattern(char character, bool ignoreCase)
		{
			Character = character;
			IgnoreCase = ignoreCase;
		}

		private char _character;
		/// <summary>
		/// Gets or sets the character to match. Setting the character '\0' (0) will 
		/// match any character. Default is <b>\0</b>.
		/// </summary>
		/// <serialization member="true" />
		#if DC
		[DataMember]
		#endif
		public char Character
		{
			get { return _character; }
			set { _character = value; }
		}

		private bool _ignoreCase;
		/// <summary>
		/// Gets or sets the case sensitive option. Default is <b>true</b>.
		/// </summary>
		/// <serialization member="true" />
		#if DC
		[DataMember]
		#endif
		public bool IgnoreCase
		{
			get { return _ignoreCase; }
			set { _ignoreCase = value; }
		}

		/// <inheritdoc />
		protected override Match OnMatch(MatchContext context)
		{
			if (_character == '\0')	// any char
			{
				if (context.IsEos)
					return Error(context);
				var ch = context.Current;
				context.NextCharacter();
				return Success(context, ch);
			}
			else
			{
				if (_ignoreCase)
				{
					if (char.ToUpperInvariant(context.Current) != char.ToUpperInvariant(_character))
						return Error(context);
				}
				else
				{
					if (context.Current != _character)
						return Error(context);
				}

				var ch = context.Current;
				context.NextCharacter();
				return Success(context, ch);
			}
		}

		/// <inheritdoc />
		public override string HelpString
		{
			get
			{
				if (_character == '\0')
					return "Any character";
				else
					return "Character '" + _character + "'";
			}
		}
	}
}
